<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.nan_to_num &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Mathematical functions" href="../routines.math.html" >
    <link rel="next" title="numpy.real_if_close" href="numpy.real_if_close.html" >
    <link rel="prev" title="numpy.fmin" href="numpy.fmin.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.math.html" accesskey="U">Mathematical functions</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.real_if_close.html" title="numpy.real_if_close"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.fmin.html" title="numpy.fmin"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.fmin.html"
                        title="previous chapter">numpy.fmin</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.real_if_close.html"
                        title="next chapter">numpy.real_if_close</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-nan-to-num">
<h1>numpy.nan_to_num<a class="headerlink" href="#numpy-nan-to-num" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.nan_to_num">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">nan_to_num</code><span class="sig-paren">(</span><em class="sig-param">x</em>, <em class="sig-param">copy=True</em>, <em class="sig-param">nan=0.0</em>, <em class="sig-param">posinf=None</em>, <em class="sig-param">neginf=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/lib/type_check.py#L368-L485"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.nan_to_num" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace NaN with zero and infinity with large finite numbers (default
behaviour) or with the numbers defined by the user using the <a class="reference internal" href="../constants.html#numpy.nan" title="numpy.nan"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nan</span></code></a>, 
<em class="xref py py-obj">posinf</em> and/or <em class="xref py py-obj">neginf</em> keywords.</p>
<p>If <em class="xref py py-obj">x</em> is inexact, NaN is replaced by zero or by the user defined value in
<a class="reference internal" href="../constants.html#numpy.nan" title="numpy.nan"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nan</span></code></a> keyword, infinity is replaced by the largest finite floating point 
values representable by <code class="docutils literal notranslate"><span class="pre">x.dtype</span></code> or by the user defined value in 
<em class="xref py py-obj">posinf</em> keyword and -infinity is replaced by the most negative finite 
floating point values representable by <code class="docutils literal notranslate"><span class="pre">x.dtype</span></code> or by the user defined 
value in <em class="xref py py-obj">neginf</em> keyword.</p>
<p>For complex dtypes, the above is applied to each of the real and
imaginary components of <em class="xref py py-obj">x</em> separately.</p>
<p>If <em class="xref py py-obj">x</em> is not inexact, then no replacements are made.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>x</strong><span class="classifier">scalar or array_like</span></dt><dd><p>Input data.</p>
</dd>
<dt><strong>copy</strong><span class="classifier">bool, optional</span></dt><dd><p>Whether to create a copy of <em class="xref py py-obj">x</em> (True) or to replace values
in-place (False). The in-place operation only occurs if
casting to an array does not require a copy.
Default is True.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.13.</span></p>
</div>
</dd>
<dt><strong>nan</strong><span class="classifier">int, float, optional</span></dt><dd><p>Value to be used to fill NaN values. If no value is passed 
then NaN values will be replaced with 0.0.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.17.</span></p>
</div>
</dd>
<dt><strong>posinf</strong><span class="classifier">int, float, optional</span></dt><dd><p>Value to be used to fill positive infinity values. If no value is 
passed then positive infinity values will be replaced with a very
large number.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.17.</span></p>
</div>
</dd>
<dt><strong>neginf</strong><span class="classifier">int, float, optional</span></dt><dd><p>Value to be used to fill negative infinity values. If no value is 
passed then negative infinity values will be replaced with a very
small (or negative) number.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.17.</span></p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>out</strong><span class="classifier">ndarray</span></dt><dd><p><em class="xref py py-obj">x</em>, with the non-finite values replaced. If <a class="reference internal" href="numpy.copy.html#numpy.copy" title="numpy.copy"><code class="xref py py-obj docutils literal notranslate"><span class="pre">copy</span></code></a> is False, this may
be <em class="xref py py-obj">x</em> itself.</p>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="numpy.isinf.html#numpy.isinf" title="numpy.isinf"><code class="xref py py-obj docutils literal notranslate"><span class="pre">isinf</span></code></a></dt><dd><p>Shows which elements are positive or negative infinity.</p>
</dd>
<dt><a class="reference internal" href="numpy.isneginf.html#numpy.isneginf" title="numpy.isneginf"><code class="xref py py-obj docutils literal notranslate"><span class="pre">isneginf</span></code></a></dt><dd><p>Shows which elements are negative infinity.</p>
</dd>
<dt><a class="reference internal" href="numpy.isposinf.html#numpy.isposinf" title="numpy.isposinf"><code class="xref py py-obj docutils literal notranslate"><span class="pre">isposinf</span></code></a></dt><dd><p>Shows which elements are positive infinity.</p>
</dd>
<dt><a class="reference internal" href="numpy.isnan.html#numpy.isnan" title="numpy.isnan"><code class="xref py py-obj docutils literal notranslate"><span class="pre">isnan</span></code></a></dt><dd><p>Shows which elements are Not a Number (NaN).</p>
</dd>
<dt><a class="reference internal" href="numpy.isfinite.html#numpy.isfinite" title="numpy.isfinite"><code class="xref py py-obj docutils literal notranslate"><span class="pre">isfinite</span></code></a></dt><dd><p>Shows which elements are finite (not NaN, not infinity)</p>
</dd>
</dl>
</div>
<p class="rubric">Notes</p>
<p>NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
(IEEE 754). This means that Not a Number is not equivalent to infinity.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)</span>
<span class="go">1.7976931348623157e+308</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)</span>
<span class="go">-1.7976931348623157e+308</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">)</span>
<span class="go">0.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="o">-</span><span class="mi">128</span><span class="p">,</span> <span class="mi">128</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">array([ 1.79769313e+308, -1.79769313e+308,  0.00000000e+000, # may vary</span>
<span class="go">       -1.28000000e+002,  1.28000000e+002])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">nan</span><span class="o">=-</span><span class="mi">9999</span><span class="p">,</span> <span class="n">posinf</span><span class="o">=</span><span class="mi">33333333</span><span class="p">,</span> <span class="n">neginf</span><span class="o">=</span><span class="mi">33333333</span><span class="p">)</span>
<span class="go">array([ 3.3333333e+07,  3.3333333e+07, -9.9990000e+03, </span>
<span class="go">       -1.2800000e+02,  1.2800000e+02])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="nb">complex</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="nb">complex</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">)])</span>
<span class="go">array([  1.79769313e+308,  -1.79769313e+308,   0.00000000e+000, # may vary</span>
<span class="go">     -1.28000000e+002,   1.28000000e+002])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">array([  1.79769313e+308 +0.00000000e+000j, # may vary</span>
<span class="go">         0.00000000e+000 +0.00000000e+000j,</span>
<span class="go">         0.00000000e+000 +1.79769313e+308j])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">nan</span><span class="o">=</span><span class="mi">111111</span><span class="p">,</span> <span class="n">posinf</span><span class="o">=</span><span class="mi">222222</span><span class="p">)</span>
<span class="go">array([222222.+111111.j, 111111.     +0.j, 111111.+222222.j])</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>